const fs = require('fs');
const util = require('util');
const path = require('path');
const crypto = require('crypto');
const merger = require('json-merger');
const readConfig = require('read-config-ng');
const debugWebSSH2 = require('debug')('WebSSH2');

const nodeRoot = path.dirname(require.main.filename);
const configPath = path.join(nodeRoot, 'config.json');

let myConfig;

const configDefault = {
    listen: {
        ip: '0.0.0.0',
        port: 2222,
    },
    socketio: {
        serveClient: false,
        path: '/ssh/socket.io',
        origins: ['localhost:2222'],
    },
    express: {
        secret: crypto.randomBytes(20).toString('hex'),
        name: 'WebSSH2',
        resave: true,
        saveUninitialized: false,
        unset: 'destroy',
        ssh: {
            dotfiles: 'ignore',
            etag: false,
            extensions: ['htm', 'html'],
            index: false,
            maxAge: '1s',
            redirect: false,
            setHeaders(res) {
                res.set('x-timestamp', Date.now());
            },
        },
    },
    user: {
        name: "redis",
        password: "redis",
        privatekey: null,
        overridebasic: false,
    },
    ssh: {
        host: "129.211.123.68",
        port: 22,
        term: 'xterm-color',
        readyTimeout: 20000,
        keepaliveInterval: 120000,
        keepaliveCountMax: 10,
        allowedSubnets: [],
    },
    terminal: {
        cursorBlink: true,
        scrollback: 10000,
        tabStopWidth: 8,
        bellStyle: 'sound',
    },
    header: {
        text: null,
        background: 'green',
    },
    options: {
        challengeButton: true,
        allowreauth: true,
    },
    algorithms: {
        kex: [
            'ecdh-sha2-nistp256',
            'ecdh-sha2-nistp384',
            'ecdh-sha2-nistp521',
            'diffie-hellman-group-exchange-sha256',
            'diffie-hellman-group14-sha1',
        ],
        cipher: [
            'aes128-ctr',
            'aes192-ctr',
            'aes256-ctr',
            'aes128-gcm',
            'aes128-gcm@openssh.com',
            'aes256-gcm',
            'aes256-gcm@openssh.com',
            'aes256-cbc',
        ],
        hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'],
        compress: ['none', 'zlib@openssh.com', 'zlib'],
    },
    serverlog: {
        client: false,
        server: false,
    },
    accesslog: false,
    verify: false,
    safeShutdownDuration: 300,
    params: {
        getRedisUrl: "https://loyer.wang/apis/tools/cache/getRedis/config?param=requirepass"
    }
};

// test if config.json exists, if not provide error message but try to run anyway
try {
    if (!fs.existsSync(configPath)) {
        console.error(`ERROR: Missing config.json for WebSSH2. Current config: ${util.inspect(myConfig)}`);
    }
    console.info(`WebSSH2 service reading config from: ${configPath}`);
    const configFile = readConfig(configPath, {override: true});
    myConfig = merger.mergeObjects([configDefault, configFile]);
    debugWebSSH2(`Current config: ${util.inspect(myConfig)}`);
} catch (err) {
    myConfig = configDefault;
    console.error(`${err}`);
}
const config = myConfig;

if (process.env.LISTEN) config.listen.ip = process.env.LISTEN;

if (process.env.PORT) config.listen.port = process.env.PORT;

if (process.env.SOCKETIO_ORIGINS) config.socketio.origins = process.env.SOCKETIO_ORIGINS;

if (process.env.SOCKETIO_PATH) config.socketio.path = process.env.SOCKETIO_PATH;

if (process.env.SOCKETIO_SERVECLIENT) config.socketio.serveClient = process.env.SOCKETIO_SERVECLIENT;

module.exports = config;
